package com.campus.delivery.controller;

import cn.hutool.core.bean.BeanUtil;
import com.campus.common.bean.common.Resp;
import com.campus.delivery.bean.DeliveryApplyBean;
import com.campus.delivery.entity.DeliveryApply;
import com.campus.delivery.service.DeliveryApplyService;
import com.campus.delivery.vo.DeliveryApplyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "配送员入驻申请相关接口")
@RestController
@RequestMapping("/deliveryApply")
public class DeliveryApplyController {
    @Resource
    private DeliveryApplyService deliveryApplyService;

    @ApiOperation("查询配送员入驻状态")
    @GetMapping("/status")
    public Resp<Map<String, Object>> getApplyStatus(@RequestParam Integer deliveryUserId) {
        DeliveryApply apply = deliveryApplyService.getLatestByUserId(deliveryUserId);

        Map<String, Object> result = new HashMap<>();
        if (apply == null) {
            result.put("status", "未入驻");
        } else if (apply.getStatus() == 0) {
            result.put("status", "审核中");
        } else if (apply.getStatus() == 1) {
            result.put("status", "审核通过");
        } else if (apply.getStatus() == 2) {
            result.put("status", "审核拒绝");
            result.put("rejectReason", apply.getRejectReason());
        }

        return Resp.success(result);
    }

    @ApiOperation("提交入驻申请")
    @PostMapping("/submit")
    public Resp<Integer> submitApply(@Validated @RequestBody DeliveryApplyBean bean) {
        Integer id = deliveryApplyService.submitApply(bean);
        return Resp.success(id);
    }

    @ApiOperation("获取最新申请详情")
    @GetMapping("/latest")
    public Resp<DeliveryApplyVo> getLatestApply(@RequestParam Integer deliveryUserId) {
        DeliveryApply apply = deliveryApplyService.getLatestByUserId(deliveryUserId);
        if (apply == null) {
            return Resp.success(null);
        }
        DeliveryApplyVo vo = new DeliveryApplyVo();
        BeanUtil.copyProperties(apply, vo);
        return Resp.success(vo);
    }
}
